goog.provide('dk.html');

goog.require('dk.util');

goog.require('goog.dom');


/**
 * Used to extract the id from a header when the header text is something like
 * "Conclusions Section id:conclusions".
 * The two matched groups will be "Conclusions Section" and "conclusions".
 * This makes it easy to format the link as:
 * <pre>
 * &lt;h1 id="conclusions">Conclusions Section&lt;/h1>
 * </pre>
 * @type {RegExp}
 */
dk.html.idRegExp = /^(.*) id:([\w-]+)$/;

/**
 * @param {{title:string,
 *          domHelper:!goog.dom.DomHelper,
 *          id:string,
 *          byline:?{firstName:string,surname:string},
 *          isAppendix:?boolean}} options
 * @return {?string}
 */
dk.html.generateHtml = function(options) {
  var domHelper = options.domHelper;

  var buffer = [];
  var isInItemizedList = false;

  dk.util.forEachElementChild(domHelper, function(el) {
    var isListItem = dk.util.isListItem(el);
    if (isInItemizedList && !isListItem) {
      buffer.push('</ul>\n');
      isInItemizedList = false;
    }

    if (dk.util.isParagraph(el)) {
      buffer.push('<p>', dk.html.editorHtmlToPublishedHtml(el.innerHTML), '</p>\n');
    } else if (dk.util.isSourceCode(el)) {
      var textContent = goog.dom.getRawTextContent(el);      
      if (goog.string.startsWith(textContent, 'HTMLVERBATIM')) {
        buffer.push(textContent.substring('HTMLVERBATIM'.length) + '\n');
      } else if (goog.string.startsWith(textContent, 'XMLVERBATIM')) {
        // Ignore.
      } else {
        buffer.push(dk.html.formatSourceCode(el));
      }
    } else if (dk.util.isH1(el)) {
      dk.html.writeHeading(el, 1, buffer);
    } else if (dk.util.isH2(el)) {
      dk.html.writeHeading(el, 2, buffer);
    } else if (dk.util.isH3(el)) {
      dk.html.writeHeading(el, 3, buffer);
    } else if (isListItem) {
      if (!isInItemizedList) {
        buffer.push('<ul>\n');
        isInItemizedList = true;
      }
      // Indent 2 spaces.
      var indent = '  ';
      buffer.push(
          indent,
          '<li>\n  ',
          dk.html.editorHtmlToPublishedHtml(el.innerHTML),
          '\n',
          indent,
          '</li>\n');
    }
  });

  if (isInItemizedList) {
    buffer.push('</ul>\n');
    isInItemizedList = false;
  }

  return buffer.join('');
};

/**
 * @param {!Element} el
 * @param {number} level of the heading (1, 2, 3)
 * @param {!Array.<string>} buffer
 */
dk.html.writeHeading = function(el, level, buffer) {
  var text = goog.dom.getRawTextContent(el);
  text = goog.string.trim(text);
  var id;
  var match = text.match(/^(.*) id:([\w-]+)$/);
  if (match) {
    text = match[1];
    id = match[2];
  }
  buffer.push('<h', level);
  if (id) buffer.push(' id="', id, '"');
  buffer.push('>', text, '</h', level, '>\n');
};

/**
 * @param {string} html, presumably the innerHTML from an Element in the editor
 * @return {string}
 */
dk.html.editorHtmlToPublishedHtml = function(html) {
  // Replace [[http://www.google.com/ Google, Inc.]] with
  // <a href="http://www.google.com/" target="_blank">Google, Inc.</a>
  html = dk.util.formatHtmlLink(html);

  // Replace ::new term:: with <em>new term</em>
  html = dk.util.formatEm(html);
  
  // Replace `function() {}` with <code>function() {}</code>
  html = dk.util.formatBackquote(html);

  return html;  
};

/**
 * @param {Element} el
 * @return {string}
 */
dk.html.formatSourceCode = function(el) {
  var textContent = goog.dom.getRawTextContent(el);
  return '<pre class="html-source-code">' + el.innerHTML + '</pre>';
};
